package de.slothsoft.wasbinich.swingclient.common;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

import de.slothsoft.wasbinich.swingclient.res.Messages;

public class UppercaseDocumentFilter extends DocumentFilter {

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr)
	    throws BadLocationException {
	fb.insertString(offset, toUpperCase(text), attr);
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
	    throws BadLocationException {
	fb.replace(offset, length, toUpperCase(text), attrs);
    }

    private String toUpperCase(String text) {
	return text == null ? null : text.toUpperCase(Messages.getLocale());
    }
}